import ApiFetchers from "@/apis/api_fetchers"
import { TODAY } from "@/pages/TransactionRecordPage/constants"
import { useEffect, useState } from "react"
import { useSearchParams } from "react-router-dom"
import { EnumBetStatus } from "@/pages/BetRecordPage/types/bet"
import useCursor from "@/pages/BetRecordPage/hooks/usePage"
import { TypeBetRecordItem, FetchUserResult, QueryParams } from "@/pages/BetRecordPage/types/bet"

const useBetRecord = () => {
    const [params, setParams] = useSearchParams()
    const status = params.get("status") || EnumBetStatus.All
    const time_flag = params.get("time_flag") || TODAY

    const [payload, setPayload] = useState<QueryParams>({})

    const {
        result,
        list,
        hasMore,
        isLoading,
        getList: getBetRecordList,
        total,
    } = useCursor<QueryParams, TypeBetRecordItem, FetchUserResult>(
        ApiFetchers.getGameBetList,
        payload,
    )

    useEffect(() => {
        setPayload({
            page_size: 10,
            status,
            time_flag,
        })
    }, [status, time_flag])

    return {
        result,
        total,
        list,
        status,
        time_flag,
        isLoading,
        hasMore,
        setParams,
        getBetRecordList,
    }
}

export default useBetRecord
